package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.SysSchoolScore;
import com.ruoyi.system.service.ISysSchoolStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysSchoolStudent;
import com.ruoyi.system.mapper.SysSchoolClassMapper;
import com.ruoyi.system.domain.SysSchoolClass;
import com.ruoyi.system.service.ISysSchoolClassService;

/**
 * 班级信息Service业务层处理
 * 
 * @author 杨皓焜
 * @date 2021-01-31
 */
@Service
public class SysSchoolClassServiceImpl implements ISysSchoolClassService 
{
    @Autowired
    private SysSchoolClassMapper sysSchoolClassMapper;
    @Autowired
    private ISysSchoolStudentService sysSchoolStudentService;

    /**
     * 查询班级信息
     * 
     * @param id 班级信息ID
     * @return 班级信息
     */
    @Override
    public SysSchoolClass selectSysSchoolClassById(Long id)
    {
        return sysSchoolClassMapper.selectSysSchoolClassById(id);
    }

    /**
     * 查询班级信息列表
     * 
     * @param sysSchoolClass 班级信息
     * @return 班级信息
     */
    @Override
    public List<SysSchoolClass> selectSysSchoolClassList(SysSchoolClass sysSchoolClass)
    {
        return sysSchoolClassMapper.selectSysSchoolClassList(sysSchoolClass);
    }

    /**
     * 新增班级信息
     * 
     * @param sysSchoolClass 班级信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysSchoolClass(SysSchoolClass sysSchoolClass)
    {
        int rows = sysSchoolClassMapper.insertSysSchoolClass(sysSchoolClass);
        insertSysSchoolStudent(sysSchoolClass);
        return rows;
    }

    /**
     * 修改班级信息
     * 
     * @param sysSchoolClass 班级信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysSchoolClass(SysSchoolClass sysSchoolClass)
    {
        addStudent(sysSchoolClass);
        return sysSchoolClassMapper.updateSysSchoolClass(sysSchoolClass);
    }

    private void addStudent(SysSchoolClass sysSchoolClass) {
        Long id = sysSchoolClass.getId();
        List<SysSchoolStudent> students = sysSchoolStudentService.selectSysSchoolStudentByClassId(id);
        sysSchoolClassMapper.deleteSysSchoolStudentByClassId(id);
        if (StringUtils.isNotNull(students))
        {
            List<SysSchoolStudent> list = new ArrayList<>();
            for (SysSchoolStudent student : students)
            {
                student.setClassId(id);
                list.add(student);
            }
            if (list.size() > 0)
            {
                sysSchoolClassMapper.batchSysSchoolStudent(list);
            }
        }
    }

    /**
     * 批量删除班级信息
     * 
     * @param ids 需要删除的班级信息ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysSchoolClassByIds(Long[] ids)
    {
        sysSchoolStudentService.deleteSysSchoolStudentByClassIds(ids);
        return sysSchoolClassMapper.deleteSysSchoolClassByIds(ids);
    }

    /**
     * 删除班级信息信息
     * 
     * @param id 班级信息ID
     * @return 结果
     */
    @Override
    public int deleteSysSchoolClassById(Long id)
    {
        sysSchoolClassMapper.deleteSysSchoolStudentByClassId(id);
        return sysSchoolClassMapper.deleteSysSchoolClassById(id);
    }

    /**
     * 新增学员信息信息
     * 
     * @param sysSchoolClass 班级信息对象
     */
    public void insertSysSchoolStudent(SysSchoolClass sysSchoolClass)
    {
        List<SysSchoolStudent> sysSchoolStudentList = sysSchoolClass.getSysSchoolStudentList();
        Long id = sysSchoolClass.getId();
        if (StringUtils.isNotNull(sysSchoolStudentList))
        {
            List<SysSchoolStudent> list = new ArrayList<SysSchoolStudent>();
            for (SysSchoolStudent sysSchoolStudent : sysSchoolStudentList)
            {
                sysSchoolStudent.setClassId(id);
                list.add(sysSchoolStudent);
            }
            if (list.size() > 0)
            {
                sysSchoolClassMapper.batchSysSchoolStudent(list);
            }
        }
    }
}
